﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsusergradeServices : Repository<corecmsusergrade>, IcorecmsusergradeServices
    {
        public bool InsertAsync(corecmsusergrade entity)
        {
            return base.Change<corecmsusergrade>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsusergrade> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsusergrade entity)
        {
            return base.Change<corecmsusergrade>().Update(a => new corecmsusergrade()
            {
                storeId = entity.storeId,
                corpId = entity.corpId,
                title = entity.title,
                isDefault = entity.isDefault,
                isOpenUpgrade = entity.isOpenUpgrade,
                developNum = entity.developNum,
                expenditureMoney = entity.expenditureMoney,
                rechargeAmount = entity.rechargeAmount,
                prioritySortId = entity.prioritySortId,
                isDelete = entity.isDelete,
                createTime = entity.createTime
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsusergrade> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsusergrade>().Update(a => new corecmsusergrade()
                {
                    storeId = item.storeId,
                    corpId = item.corpId,
                    title = item.title,
                    isDefault = item.isDefault,
                    isOpenUpgrade = item.isOpenUpgrade,
                    developNum = item.developNum,
                    expenditureMoney = item.expenditureMoney,
                    rechargeAmount = item.rechargeAmount,
                    prioritySortId = item.prioritySortId,
                    isDelete = item.isDelete,
                    createTime = item.createTime
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsusergrade>().Update(a => new corecmsusergrade()
            {
                isDelete = true
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsusergrade>().Update(a => new corecmsusergrade()
                {
                    isDelete = true
                }, a => a.id == id);
            }
            return returnKey;
        }


        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsusergrade> GetCaChe()
        {
            return base.GetList();
        }

        #endregion
        public List<corecmsusergrade> SelectByCorpId(int corpId, int storeId)
        {
            return base.GetList(a => a.corpId == corpId && a.storeId == storeId && a.isDelete == false).OrderBy(a => a.id).ToList();
        }

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsusergrade> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsusergrade> custos = new List<corecmsusergrade>();
            var where = Expressionable.Create<corecmsusergrade>();
            where = where.And(a => a.isDelete == false);
            //if (!string.IsNullOrEmpty(sName))
            //{
            //    where = where.And(a => a.name.Contains(sName))
            //        .Or(a => a.number.Contains(sName)).Or(a => a.name.Contains(sName));
            //}
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsusergrade> QueryAsync(long typeId)
        {
            List<corecmsusergrade> list = new List<corecmsusergrade>();
            var where = Expressionable.Create<corecmsusergrade>();
            where = where.And(a => a.isDelete == false && a.id > 0);
            if (typeId > 0)
            {
                where = where.And(a => a.id == typeId);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        #endregion

    }
}
